package cn.mengfly.oauthserver.cfg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

/**
 * @author Mengfly
 * @date 2021/3/24 21:34
 */
@Configuration
public class OAuthConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private BCryptPasswordEncoder encoder;



    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // 通过内存方式设置认证的客户端
        clients.inMemory().withClient("springclouddemo")
                .secret(encoder.encode("scdsecret"))
                .authorizedGrantTypes("refresh_token", "password", "client_credentials")
                .scopes("any");
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 设置用户及认证的实现
        endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService);
    }
}
